const { md5 } = require("tools-factory-tang/salt");
const mongoose = require("mongoose");

// 登录方法
const login = async ({ username, password }) => {
    password = md5(password);
    const usersModel = mongoose.model("users");
    //查询账号密码
    return await usersModel.findOne({ username, password }); //返回查询数据库users集合中第一个查询到的data数据
}

// 根据用户输入添加用户信息
const reg = async ({ username, password, home, iphone }) => {
    // 密码加密
    password = md5(password);
    const usersModel = mongoose.model("users");
    // 在数据库中，查找注册的用户名是否存在
    let status = await usersModel.find({ username: username });
    // 判断用户是否已经存在
    if (status.length == 0) {//判断数据是否存在,不存就将数据添加进去,然后返回状态值为true,非则返回false
        await usersModel.create({ username, password, home, iphone });
        return true
    } else {
        return false
    }
    // 这是注册结果是否成功的Boolean值
}




const get = async ({
    curPage,
    eachPage
}) => {
    // 强制转换类型 number转string
    (curPage = ~~curPage), (eachPage = ~~eachPage);
    const data = {
        total: 0,
        maxPage: 0,
        rows: [],
    }
    //mongoose.model传三个参数是创建  传一个参数是获取
    const userModel = mongoose.model("users");
    const addressModel = mongoose.model("address")
    //get 列表数据

    // 获取当前model映射的那个集合里存在的文档总数
    data.total = await userModel.countDocuments();
    // Math.ceil往上取整
    data.maxPage = Math.ceil(data.total / eachPage);
    // find查询所有数据 findOne查询满意结果的第一条
    // skip跳过多少条开始查找 
    //limit 查询多少条数据
    // sort 排序
    //exec 立即执行
    const skipNum = (curPage - 1) * eachPage;
    data.rows = await userModel.find().skip(skipNum).limit(eachPage).exec()
    data.Address = await addressModel.find()
    return data;
};



const updataHome = async ({ _id, home }) => {
    const userModel = mongoose.model("users");

    // await userModel.updateOne({ _id }, { home })
    const { allAddress } = await userModel.findOne({ _id })
    await !allAddress.some((item) => { return item === home }) ? allAddress.push(home) : ""

    const data = await userModel.updateOne({ _id }, { home, allAddress })

    return data
}


// 删除一个收藏的地址
const deleOneaddress = async ({ _id, addressId }) => {
    const userModel = mongoose.model("users");
    const { allAddress } = await userModel.findOne({ _id })
    // await !allAddress.some((item)=>{return item===addressId})?allAddress.pull(addressId):""
    allAddress.pull(addressId)
    const data = await userModel.updateOne({ _id }, { allAddress })
    return data

}
const fromName = async ({ name }) => {
    const userModel = mongoose.model("users");
    const result = await userModel.find({ username: { $regex: name, $options: 'i' } })
    return result
}


// 上传头像
const addPoster = async ({ _id, url }) => {
    const userModel = mongoose.model("users");
    return await userModel.updateOne({ _id }, { Poster: url })
}

// 获取一个用户信息
const getOneUser = async ({ _id }) => {
    const userModel = mongoose.model("users");
    let data = await userModel.findOne({ _id }).populate("home")
    // model传一个参数时表示获取这个model对象
    const addressModel = mongoose.model("address");
    // 添加电影数据 Child_type: { "$in": chilarr }
    data.allAddress= await addressModel.find({_id:{ "$in": data.allAddress }});
    return data
}





module.exports = { login, reg, get, updataHome, deleOneaddress, fromName, addPoster, getOneUser };